package com.one.ui;

import com.one.component.BackGroundPanel;
import com.one.domain.ResultInfo;
import com.one.net.FailListener;
import com.one.net.ImageRequestUtils;
import com.one.net.PostUtils;
import com.one.utils.JsonUtils;
import com.one.utils.ScreenUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class RegisterInterface {
    JFrame jf = new JFrame();
    final int WIDTH = 500;
    final int HEIGHT = 400;

    // 组装视图
    public void init() throws IOException {
        jf.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);
        jf.setResizable(false);
        jf.setIconImage(ImageIO.read(new File("img/logo.png")));
        BackGroundPanel bgPanel = new BackGroundPanel(ImageIO.read(new File("img/regist.jpg")));
        bgPanel.setBounds(0,0,WIDTH,HEIGHT);
        Box vBox = Box.createVerticalBox();

        // 组装用户名
        Box uBox = Box.createHorizontalBox();
        JLabel uLabel = new JLabel("用户名: ");
        JTextField uField = new JTextField(15);
        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(uField);
        // 组装密码
        Box pBox = Box.createHorizontalBox();
        JLabel pLabel = new JLabel("密    码: ");
        JTextField pField = new JTextField(15);
        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(pField);
        // 组装手机号
        Box phoneBox = Box.createHorizontalBox();
        JLabel phoneLabel = new JLabel("手机号: ");
        JTextField phoneField = new JTextField(15);
        phoneBox.add(phoneLabel);
        phoneBox.add(Box.createHorizontalStrut(20));
        phoneBox.add(phoneField);
        // 性别
        Box gBox = Box.createHorizontalBox();
        JLabel gLabel = new JLabel("性    别: ");
        JRadioButton maleBtn = new JRadioButton("男",true);
        JRadioButton femaleBtn = new JRadioButton("女",false);
        // 实现单选的效果
        ButtonGroup bGroup = new ButtonGroup();
        bGroup.add(maleBtn);
        bGroup.add(femaleBtn);
        gBox.add(gLabel);
        gBox.add(Box.createHorizontalStrut(20));
        gBox.add(maleBtn);
        gBox.add(femaleBtn);
        gBox.add(Box.createHorizontalStrut(120));
        // 组装验证码
        Box cBox = Box.createHorizontalBox();
        JLabel cLabel = new JLabel("验证码");
        JTextField cField = new JTextField();
        JLabel cImg = new JLabel(new ImageIcon(ImageRequestUtils.getImage("http://localhost:8080/code/getCheckCode")));
        // 给某个组件设置提示信息
        cImg.setToolTipText("点击刷新");
        cImg.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                cImg.setIcon(new ImageIcon(ImageRequestUtils.getImage("http://localhost:8080/code/getCheckCode")));
                cImg.updateUI();
            }
        });
        cBox.add(cLabel);
        cBox.add(Box.createHorizontalStrut(20));
        cBox.add(cField);
        cBox.add(cImg);
        // 组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton registBtn = new JButton("注册");
        JButton backBtn = new JButton("返回登录页面");
        registBtn.addActionListener(e -> {
            // 获取用户录入的信息
            String username = uField.getText().trim();
            String password = pField.getText().trim();
            String phone = phoneField.getText().trim();
            String gender = bGroup.isSelected(maleBtn.getModel()) ? maleBtn.getText() : femaleBtn.getText();
            String checkCode = cField.getText().trim();
            HashMap<String, String> params = new HashMap<>();
            params.put("username",username);
            params.put("password",password);
            params.put("phone",phone);
            params.put("gender",gender);
            params.put("checkCode",checkCode);

            // 访问后台接口
            PostUtils.postWithParams("http://localhost:8080/user/register", params, result -> {
                ResultInfo info = JsonUtils.parseResult(result);
                if (info.isFlag()) {
                    // 注册成功
                    JOptionPane.showMessageDialog(jf,"注册成功,即将返回登录页面");
                    new MainInterface().init();
                    jf.dispose();
                } else {
                    // 注册失败
                    JOptionPane.showMessageDialog(jf,info.getMessage());
                }
            }, () -> {
                JOptionPane.showMessageDialog(jf,"网络异常,请稍后重试");
            });

        });
        backBtn.addActionListener(e -> {
            // 返回到登录页面
            try {
                new MainInterface().init();
                jf.dispose();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });

        btnBox.add(registBtn);
        btnBox.add(Box.createHorizontalStrut(80));
        btnBox.add(backBtn);
        // 将水平Box添加到垂直Box中
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(phoneBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(gBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(cBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnBox);
        // 将垂直Box添加到JPanel画布中
        bgPanel.add(vBox);
        jf.add(bgPanel);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}
